﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO;


namespace DAO
{
    public class NhapHangDAO
    {
        public void ThemNhapHang(NhapHangDTO nh)
        {
            SqlConnection cn = DataProvider.ConnectionData();
            string sql = "sp_ThemHoaDonNhap";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@MaNCC",nh.MaNCC);
            cmd.Parameters.Add("@ThoiGian", nh.ThoiGian.ToShortDateString());
            cmd.Parameters.Add("@ThanhToan", nh.ThanhToan);
            cmd.Parameters.Add("@CongNo", nh.CongNo);
            cmd.Parameters.Add("@ThanhTien", nh.TongTien);     
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public void ThemChiTietNhapHang(ChiTietNhapHangDTO nh)
        {
            SqlConnection cn = DataProvider.ConnectionData();
            string sql = "sp_ThemChiTietNhapHang";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@MaHDN", nh.MaHDN);
            cmd.Parameters.Add("@MaHoa", nh.MaHoa);
            cmd.Parameters.Add("@SoLuong", nh.SoLuong);
            cmd.Parameters.Add("@Gia", nh.Gia);
            cmd.Parameters.Add("@ThanhTien", nh.ThanhTien);
            cmd.Parameters.Add("@SoLuongLoi", nh.SoLuongLoi);
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public int LayMaNhapHang()
        {
            int maNH;
            SqlConnection cn = DataProvider.ConnectionData();
            string sql = "sp_LayMaNhapHang";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@maNH", SqlDbType.Int);
            cmd.Parameters["@maNH"].Direction = ParameterDirection.Output;
            cmd.Parameters["@maNH"].Value = 0;
            cmd.ExecuteNonQuery();
            maNH = Int32.Parse(cmd.Parameters["@maNH"].Value.ToString());
            cn.Close();
            return maNH;
        }
        public void ThemSoLuongHoa(ChiTietNhapHangDTO ctnh)
        {
            SqlConnection cn = DataProvider.ConnectionData();
            string sql = "sp_ThemSoLuongHoa";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@maHoa", ctnh.MaHoa);
            cmd.Parameters.Add("@soLuong", ctnh.SoLuong);
            cmd.ExecuteNonQuery();
            cn.Close();
        }
    }
}
